package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysLogininforVo;
import com.ruoyi.system.domain.bo.SysLogininforBo;
import com.ruoyi.system.service.ISysLogininforService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系统访问记录
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/logininfor")
public class SysLogininforController extends BaseController {

    private final ISysLogininforService iSysLogininforService;

    /**
     * 查询系统访问记录列表
     */
    @SaCheckPermission("system:logininfor:list")
    @GetMapping("/list")
    public TableDataInfo<SysLogininforVo> list(SysLogininforBo bo, PageQuery pageQuery) {
        return iSysLogininforService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出系统访问记录列表
     */
    @SaCheckPermission("system:logininfor:export")
    @Log(title = "系统访问记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysLogininforBo bo, HttpServletResponse response) {
        List<SysLogininforVo> list = iSysLogininforService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统访问记录", SysLogininforVo.class, response);
    }

    /**
     * 获取系统访问记录详细信息
     *
     * @param infoId 主键
     */
    @SaCheckPermission("system:logininfor:query")
    @GetMapping("/{infoId}")
    public R<SysLogininforVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long infoId) {
        return R.ok(iSysLogininforService.queryById(infoId));
    }

    /**
     * 新增系统访问记录
     */
    @SaCheckPermission("system:logininfor:add")
    @Log(title = "系统访问记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysLogininforBo bo) {
        return toAjax(iSysLogininforService.insertByBo(bo));
    }

    /**
     * 修改系统访问记录
     */
    @SaCheckPermission("system:logininfor:edit")
    @Log(title = "系统访问记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysLogininforBo bo) {
        return toAjax(iSysLogininforService.updateByBo(bo));
    }

    /**
     * 删除系统访问记录
     *
     * @param infoIds 主键串
     */
    @SaCheckPermission("system:logininfor:remove")
    @Log(title = "系统访问记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] infoIds) {
        return toAjax(iSysLogininforService.deleteWithValidByIds(Arrays.asList(infoIds), true));
    }
}
